package com.zat.mes.andrmes.itemdecoration;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.zat.mes.andrmes.R;

/**
 * recyclerview 时间轴
 */
public class TimeItemDecoration extends RecyclerView.ItemDecoration {


    private int circle_radius;
    private Paint paint;
    private int itemView_leftinterval;
    //    private int icon_width;
//    private Bitmap bitmap;
    private Context context;

    public TimeItemDecoration(Context context) {
        this.context = context;
        paint = new Paint();
        //画笔颜色：#FFD7D7D7
        paint.setColor(ContextCompat.getColor(context, R.color.color_line_grey));
        //设置画笔宽度，可用于修改轴的宽度
        paint.setStrokeWidth(dip2px(context, 1));
        paint.setStyle(Paint.Style.STROKE);//空心
        //获取图片
//        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.circle_grey_time);
        //矩形左侧偏移量px
        itemView_leftinterval = dip2px(context, 32);
        //最后一个圆点的半径
        circle_radius = dip2px(context, 4);
        //图标所在正方形宽高(一半)
//        icon_width = dip2px(context,8);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //重中之重，时间轴实现的核心，可以理解为，在某一个方向将子布局推移
        //参数含义：相对于父布局左偏移，上偏移，右偏移，下偏移 （个人理解：变相margin值）
        outRect.set(itemView_leftinterval, 0, 0, 0);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        c.drawColor(ContextCompat.getColor(context, R.color.color_white));
        int childCount = parent.getChildCount();
        //遍历子布局，为每一个子布局进行绘制
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            //下面两个参数是计算我们的矩形相对于父布局所在位置
            int centerx = child.getLeft() - itemView_leftinterval / 2;
//            int centery = child.getTop() + child.getHeight() / 2;
            int centery = child.getTop() + dip2px(context, 7);//文本大约14dp
            //计算图标展示的正方形大小（相对于父布局位置）
            Rect rect = new Rect(centerx - circle_radius, centery - circle_radius
                    , centerx + circle_radius, centery + circle_radius);

            /**
             * 上半轴绘制
             */
            //上端点坐标（X，Y）
            float upLine_up_x = centerx;
            float upLine_up_y = child.getTop();
            //下端点坐标（X，Y）
            float upLine_bottom_x = centerx;
            float upLine_bottom_y = centery - circle_radius;

            /**
             * 如果是最后一个item绘制上半轴需要加长
             */
            //上端点坐标（X，Y）
            float upLine_last_up_x = centerx;
            float upLine_last_up_y = child.getTop();
            //下端点坐标（X，Y）
            float upLine_last_bottom_x = centerx;
            float upLine_last_bottom_y = centery - circle_radius;

            /**
             * 下半轴绘制
             */
            //上端点（X,Y）
            float bottomLine_up_x = centerx;
            float bottom_up_y = centery + circle_radius;
            //下端点（X,Y）
            float bottomLine_bottom_x = centerx;
            float bottomLine_bottom_y = child.getBottom();

            //顶部不绘制上端线，底部不绘制下端线且改变图标为实心圆
            int index = parent.getChildAdapterPosition(child);
            if (childCount == 1) {
                c.drawCircle(centerx, centery, circle_radius, paint);
            } else if (index == 0) {
                c.drawCircle(centerx, centery, circle_radius, paint);
                c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, paint);
            } else if (index + 1 == childCount) {
                c.drawCircle(centerx, centery, circle_radius, paint);
                c.drawLine(upLine_last_up_x, upLine_last_up_y, upLine_last_bottom_x, upLine_last_bottom_y, paint);
            } else {
                c.drawCircle(centerx, centery, circle_radius, paint);
                c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, paint);
                c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, paint);
            }
        }
    }


    /**
     * dp->px
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

